<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form
      :inline="true"
      :model="query"
      class="demo-form-inline"
    >
      <el-form-item label="名称">
        <el-input
          v-model="query.name"
          placeholder="名称"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="search"
        >查询</el-button>
        <el-button
          type="primary"
          @click="handleAdd"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="name"
        label="名称"
      />
      <el-table-column
        prop="storeName"
        label="存储名"
      />
      <el-table-column
        lable="图片"
        width="200"
      >
        <template slot-scope="scope">
          <el-card style="width:200px;">
            <el-image :src="scope.row.url" />
          </el-card>
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
      />

      <el-table-column
        label="操作"
        align="right"
        width="180"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="query.total"
    />

    <!-- 新增、编辑用户 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      width="30%"
    >
      <el-form
        ref="form"
        :model="form"
      >
        <el-form-item
          label="名称"
          prop="name"
          label-width="120px"
        >
          <el-input
            v-model="form.name"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="排序"
          prop="sort"
          label-width="120px"
        >
          <el-input
            v-model="form.sort"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="图片"
          prop="nickName"
          label-width="120px"
        >
          <el-upload
            action=""
            :auto-upload="false"
            :on-change="fileChange"
            :on-remove="fileRemove"
            :file-list="fileList"
          >
            <el-button
              size="small"
              type="primary"
            >点击上传</el-button>
            <div
              slot="tip"
              class="el-upload__tip"
            >只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          v-if="title === '新增'"
          type="primary"
          @click="add"
        >确 定</el-button>
        <el-button
          v-else-if="title === '编辑'"
          type="primary"
          @click="edit"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { getPage, add, del } from '@/api/carousel'

export default {
  data() {
    return {
      tableData: [],
      query: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
        nickName: '',
        userName: ''
      },
      dialogFormVisible: false,
      form: {},
      title: '',
      departments: []
    }
  },
  mounted() {
    this.getPage()
  },
  methods: {
    getPage() {
      getPage(this.query).then((res) => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
        }
      })
    },
    search() {
      this.getPage()
    },
    // 文件状态改变时的钩子
    fileChange(file, fileList) {
      // 删除重复上传文件
      for (let i = 0; i < fileList.length - 1; i += 1) {
        for (let j = 0; j < fileList.length; j += 1) {
          if (fileList[i].name === fileList[j].name) {
            fileList.splice(i, 1)
            this.isRepeat = true
            break
          }
        }
      }
      this.fileList = fileList
      // 避免值在同一个事件队列中被一次性更新，这样会监听不到isRepeat的更改
      this.$nextTick(() => {
        this.isRepeat = false
      })
    },
    // 上传文件删除时调用
    fileRemove(file, fileList) {
      this.fileList = fileList
    },
    handleAdd() {
      this.title = '新增'
      this.form = {}
      this.dialogFormVisible = true
    },
    add() {
      const formData = new FormData()
      formData.append('name', this.form.name)
      formData.append('sort', this.form.sort)
      formData.append('file', this.fileList[0].raw)

      add(formData).then((res) => {
        if (res.code === 0) {
          this.$message({
            message: '添加用户成功',
            type: 'success'
          })
          this.dialogFormVisible = false
          this.getPage()
        }
      })
    },

    handleDelete(row) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          del({ id: row.id }).then((res) => {
            if (res.code === 0) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getPage()
            } else {
              this.$message({
                type: 'danger',
                message: '删除失败!'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
